﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace OrganizedConfusion.Game_Objects
{
    public class BaseObject
    {
        public enum OBJ_TYPE { OBJ_BLOCK = 0, OBJ_FLAG = 1, OBJ_FLOOR = 2, OBJ_JUMPBOX = 3, OBJ_LAUNCHCANNON = 4, OBJ_RAMPS = 5, OBJ_SPIKE = 6, OBJ_TURRET = 7, OBJ_PLAYER = 8, OBJ_PROJECTILE = 9, OBJ_NUM };
        Vector2 position;
        int image = -1;
        public Vector2 ImageDimensions { get; set; }
        internal OBJ_TYPE type;
        public Vector2 Origin;
        public float Scale;

        public BaseObject() { }

        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }

        public int Type
        {
            get { return (int)type; }
            set { type = (OBJ_TYPE)value; }
        }

        public int Image
        {
            get { return image; }
            //internal set { image = value; }
        }

        public void Unload()
        {
            if(image > -1)
                ContentsManager.Instance.UnloadTexture(image);
        }

        public void LoadImage(string img)
        {
            if (image > -1)
                ContentsManager.Instance.UnloadTexture(image);
            image = ContentsManager.Instance.LoadTexture(img);
            Scale = 1.0f;

            ImageDimensions = ContentsManager.Instance.GetImageDimensions(image) * Scale;
            Origin = new Vector2(ImageDimensions.X * .5f, ImageDimensions.Y * .5f);
        }

        virtual internal Rectangle CreateCollisionRect()
        {
            return new Rectangle((int)Position.X, (int)Position.Y, (int)(ImageDimensions.X * Scale), (int)(ImageDimensions.Y * Scale));
        }

        virtual internal Vector2 GetCollisionRectPosition()
        {
            return new Vector2(Position.X - Origin.X, Position.Y - Origin.Y);
        }

        public virtual bool CheckCollision(BaseObject obj)
        {
            if (this.Type != (int)OBJ_TYPE.OBJ_PROJECTILE)
            {
                if (obj.Type != (int)OBJ_TYPE.OBJ_PLAYER || this.Type == (int)OBJ_TYPE.OBJ_PLAYER)
                    return false;

                Player user = (Player)obj;

                if ((obj.Position.X + obj.ImageDimensions.X) > Position.X && obj.Position.X < Position.X + ImageDimensions.X)
                {
                    if ((obj.Position.Y + obj.ImageDimensions.Y) > Position.Y)
                    {
                        user.Velocity = new Vector2(user.Velocity.X, 0.0f);
                        user.Position = new Vector2(user.Position.X, Position.Y - user.ImageDimensions.Y);
                        return true;
                    }
                }
            }
            else
            {
                // Change to circle collision
                Rectangle rtnRect = Rectangle.Intersect(obj.CreateCollisionRect(), CreateCollisionRect());

                //if (rtnRect.Width != 0 && rtnRect.Height != 0)
                //    Do event sending here
            }

            return false;
        }

        public void ChangeScale(float scale)
        {
            Scale = scale;
            ImageDimensions = ContentsManager.Instance.GetImageDimensions(image) * Scale;
            Origin = new Vector2(ImageDimensions.X * .5f, ImageDimensions.Y * .5f);
        }

        public void ChangeOrigin(Vector2 origin) { Origin = origin; }

        public virtual void Render(SpriteBatch sb)
        {
            //sb.Draw(ContentsManager.Instance.Draw(Image), Position, Color.White);
            sb.Draw(ContentsManager.Instance.Draw(Image), Position, null, Color.White, 0.0f, Origin, Scale, SpriteEffects.None, 0.0f);
        }
    }
}
